package com.dycx.user.converter;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.common.enums.BizTypeEnum;
import com.dycx.user.dto.CollectDTO;
import com.dycx.user.entity.CollectDO;
import com.dycx.user.vo.CollectVO;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 收藏(Collect)实体转换类
 *
 * @author wuzongbin
 * @since 2023-12-29 11:37:20
 */


@Data
public class CollectConverter implements Serializable {
    public static CollectDO dtoToEntity(CollectDTO dto) {
        if (dto == null) return null;
        CollectDO entity = BeanUtil.toBean(dto, CollectDO.class);
        return entity;
    }

    public static CollectVO entityToVo(CollectDO entity) {
        if (entity == null) return null;
        CollectVO vo = BeanUtil.toBean(entity, CollectVO.class);
        vo.setBizTypeName(BizTypeEnum.converter(vo.getBizType()));

        return vo;
    }

    public static List<CollectVO> entityToVoList(List<CollectDO> entityList) {
        List<CollectVO> voList = new ArrayList<>();
        for (CollectDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<CollectVO> entityToVoPage(Page<CollectDO> page) {

        List<CollectVO> newRecords = new ArrayList<>();

        List<CollectDO> records = page.getRecords();
        for (CollectDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<CollectVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
